//
//  FragmentTableViewCellOne.m
//  A_Project_DLS151129_SQ
//
//  Created by 151129-49宋强 on 16/1/21.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "FragmentTableViewCellOne.h"

@interface FragmentTableViewCellOne ()<UIScrollViewDelegate>

@end
@implementation FragmentTableViewCellOne
- (void)dealloc
{
    [_headView release];
    [_imgView release];
    [_xinImgView release];
    [_commentView release];
    [_nameLabel release];
    [_timeLabel release];
    [_contentLabel release];
    [_commentLabel release];
    [_xinLabel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.headView = [[UIImageView alloc] init];
       // _headView.backgroundColor = [UIColor redColor];
        _headView.layer.cornerRadius = 30;
        _headView.clipsToBounds = YES;
        [self.contentView addSubview:_headView];
        [_headView release];
        
        self.nameLabel = [[UILabel alloc] init];
       // _nameLabel.backgroundColor = [UIColor blackColor];
        [self.contentView addSubview:_nameLabel];
        [_nameLabel release];
        
        self.timeLabel = [[UILabel alloc] init];
      //  _timeLabel.backgroundColor = [UIColor redColor];
        _timeLabel.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:_timeLabel];
        [_timeLabel release];
        
        self.imgView = [[UIImageView alloc] init];
      //  _imgView.backgroundColor = [UIColor redColor];
        _imgView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [_imgView addGestureRecognizer:tap];
        [self.contentView addSubview:_imgView];
        [tap release];
        [_imgView release];
        
        self.contentLabel = [[UILabel alloc] init];
      //  _contentLabel.backgroundColor = [UIColor blackColor];
        [self.contentView addSubview:_contentLabel];
        _contentLabel.numberOfLines = 0;
        [_contentLabel release];
        
        self.xinImgView = [[UIImageView alloc] init];
        _xinImgView.image = [UIImage imageNamed:@"xin.png"];
        [self.contentView addSubview:_xinImgView];
        [_xinImgView release];
        
        self.xinLabel = [[UILabel alloc] init];
        //_xinLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_xinLabel];
        [_xinLabel release];
        
        self.commentLabel = [[UILabel alloc] init];
      //  _commentLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_commentLabel];
        [_commentLabel release];
        
        self.commentView = [[UIImageView alloc] init];
       // _commentView.backgroundColor = [UIColor redColor];
        _commentView.image = [UIImage imageNamed:@"commet.png"];
        [self.contentView addSubview:_commentView];
        [_commentView release];

        self.size = self.frame.size;
    }
    return self;
}
//手势放大图片方法
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    if (![(UIImageView *)tap.view image]) {
        return;
    }
    //scrollView作为背景
    UIScrollView *bgView = [[UIScrollView alloc] init];
    bgView.frame = [UIScreen mainScreen].bounds;
    bgView.backgroundColor = [UIColor blackColor];
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
    [bgView addGestureRecognizer:tapBg];
    
    UIImageView *picView = (UIImageView *)tap.view;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = picView.image;
    imageView.frame = [bgView convertRect:picView.frame fromView:self.contentView];
    [bgView addSubview:imageView];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
    
    self.lastImageView = imageView;
    self.originalFrame = imageView.frame;
    self.scrollView = bgView;
    //最大放大比例
    self.scrollView.maximumZoomScale = 1.5;
    self.scrollView.delegate = self;
    
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = imageView.frame;
        frame.size.width = bgView.frame.size.width;
        frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
        frame.origin.x = 0;
        frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;
        imageView.frame = frame;
    }];
}

-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
    self.scrollView.contentOffset = CGPointZero;
    [UIView animateWithDuration:0.5 animations:^{
        self.lastImageView.frame = self.originalFrame;
        tapBgRecognizer.view.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [tapBgRecognizer.view removeFromSuperview];
        self.scrollView = nil;
        self.lastImageView = nil;
    }];

}
//返回可缩放的视图
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.lastImageView;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _headView.frame = CGRectMake(10, 10, 60, 60);
    _nameLabel.frame = CGRectMake(10 + 60 + 2, 20 , 150, 40);
    _timeLabel.frame = CGRectMake(self.frame.size.width - 60, 20, 100, 40);
    CGFloat ImgH = [[self class] heightWithImg:self.coverimg_wh];
    _imgView.frame = CGRectMake(10, 10 + 60 + 10, self.frame.size.width - 20, ImgH);
    CGFloat hh = [[self class] heightWithText:_contentLabel.text];
    _contentLabel.frame = CGRectMake(10, _imgView.frame.origin.y + _imgView.frame.size.height + 10, self.frame.size.width - 20, hh);
    CGRect content = _contentLabel.frame;
    _commentView.frame = CGRectMake(self.frame.size.width / 2 + 50, content.origin.y + content.size.height + 15, 20, 20);
    _commentLabel.frame = CGRectMake(_commentView.frame.origin.x + _commentView.frame.size.width + 2, _commentView.frame.origin.y, 40, 20);
    CGRect commentLabel = _commentLabel.frame;
    _xinImgView.frame = CGRectMake(commentLabel.origin.x +commentLabel.size.width + 10, commentLabel.origin.y, 20, 20);
    CGRect xin = _xinImgView.frame;
    _xinLabel.frame = CGRectMake(xin.origin.x + 20 + 2, xin.origin.y, 40, 20);

}
//图片自适应高度方法
+ (CGFloat)heightWithImg:(NSString *)bili
{
    
    NSArray *arr = [bili componentsSeparatedByString:@"*"];
    if (arr) {
        NSInteger i = [arr[0] integerValue];
        NSInteger j = [arr[1] integerValue];
        return (i / j) * 355;

    }else{
        return 0;
    }
    
    
}
//自适应Label高度方法
+ (CGFloat)heightWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:17]};
    CGSize s = CGSizeMake(355, 1000);
    CGRect r = [text boundingRectWithSize:s options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return r.size.height;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
